package com.py.inbound.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.py.api.PyCodeApi;
import com.py.inbound.domain.PyInboundDetalis;
import com.py.inbound.domain.WhInventory;
import com.py.inbound.service.IPyInboundDetalisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-12-02
 */
@RestController
@RequestMapping("/detalis")
public class PyInboundDetalisController extends BaseController {
    @Autowired
    private IPyInboundDetalisService pyInboundDetalisService;

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:detalis:list")
    @GetMapping("/list")
    public TableDataInfo list(PyInboundDetalis pyInboundDetalis) {
        startPage();
        List<PyInboundDetalis> list = pyInboundDetalisService.selectPyInboundDetalisList(pyInboundDetalis);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:detalis:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PyInboundDetalis pyInboundDetalis) {
        List<PyInboundDetalis> list = pyInboundDetalisService.selectPyInboundDetalisList(pyInboundDetalis);
        ExcelUtil<PyInboundDetalis> util = new ExcelUtil<PyInboundDetalis>(PyInboundDetalis.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @RequiresPermissions("system:detalis:query")
    @GetMapping(value = "/{rkDetailsId}")
    public AjaxResult getInfo(@PathVariable("rkDetailsId") Long rkDetailsId) {
        return success(pyInboundDetalisService.selectPyInboundDetalisByRkDetailsId(rkDetailsId));
    }


    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:detalis:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<PyInboundDetalis> pyInboundDetalis) {
        return toAjax(pyInboundDetalisService.updatePyInboundDetalis(pyInboundDetalis));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:detalis:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{rkDetailsIds}")
    public AjaxResult remove(@PathVariable Long[] rkDetailsIds) {
        return toAjax(pyInboundDetalisService.deletePyInboundDetalisByRkDetailsIds(rkDetailsIds));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @RequiresPermissions("system:detalis:add")
    @Log(title = "入库明细添加", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody List<PyInboundDetalis> pyInboundDetalis) {
//        pyInboundDetalis.forEach(s -> System.out.println(s));
//
//        System.out.println("添加入库明细:" + pyInboundDetalis);

        int i = pyInboundDetalisService.insertPyInboundDetalisList(pyInboundDetalis);
        for (PyInboundDetalis p : pyInboundDetalis
        ) {
            WhInventory whInventory = new WhInventory();
            whInventory.setLocationCode(p.getLocationCode());
            whInventory.setProductCode(p.getProductCode());
            WhInventory whInventory1 = pyInboundDetalisService.SelectWhInventoryInventoryUpperLimit(whInventory);
//            System.out.println("whInventory1.getInventoryUpperLimit():" + whInventory1.getInventoryUpperLimit());
//            System.out.println("whInventory1.getCurrentInventory() +:" + whInventory1.getCurrentInventory() + p.getQuantityThisTime());
            if (whInventory1.getInventoryUpperLimit() < whInventory1.getCurrentInventory() + p.getQuantityThisTime()) {
                return AjaxResult.warn("库存不能高于货品库存上限，不允许入库！", null);
            }

        }

        System.out.println("明细成功:" + i);
        return toAjax(i);
    }

//    @GetMapping("/selectAllPyInboundDetalisBycode")
//    public AjaxResult selectAllPyInboundDetalisBycode() {
//
//
//        return success(pyInboundDetalisService.selectAllPyInboundDetalisBycode());
//
//    }

    @GetMapping("/selectAllPyInboundDetalisBycodeXQ")
    public AjaxResult selectAllPyInboundDetalisBycodeXQ(@RequestParam("inventoryNumber") String inventoryNumber) {

//        System.out.println("入库编码:" + inventoryNumber);
        return success(pyInboundDetalisService.selectAllPyInboundDetalisBycodeXQ(inventoryNumber));

    }


}
